﻿Imports System.Data.SqlClient
Public Class tablenumber
    Dim cmd As New SqlCommand

#Region "Search and select Data"
    Public Sub refreshdata()
        Try
            grdtable.Rows.Clear()
            cmd.Connection = cn
            cmd.CommandText = "select * from tbltable where tablename like N'%" & txtsearch.Text & "%' OR tableID like N'%" & txtsearch.Text & "%'"
            Dim rd As SqlDataReader = cmd.ExecuteReader
            While rd.Read = True
                Me.grdtable.Rows.Add(rd(0), rd(1), rd(2))
            End While
            rd.Close()
        Catch ex As Exception
        End Try
    End Sub
#End Region
    Private Sub tablenumber_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        condb()
        refreshdata()
        radioname.Checked = True
        Main.lblheader.Text = "តុអាហារ"
    End Sub
#Region "search Texbox"
    Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
        refreshdata()
    End Sub
#End Region

#Region "Insert data"
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Try
            cmd = New SqlCommand
            cmd.Connection = cn
            cmd.CommandText = "INSERT INTO tblTable VALUES (N'" & txttablename.Text & "',N'" & txtdescription.Text & "')"
            cmd.ExecuteNonQuery()
            refreshdata()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
#End Region

#Region "Update data"

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        cmd = New SqlCommand
        cmd.Connection = cn
        cmd.CommandText = "update tblTable set TableName =N'" & txttablename.Text & "',Description =N'" & txtdescription.Text & "' where TableID ='" & txtid.Text & "'"
        cmd.ExecuteNonQuery()
        MessageBox.Show("Update Success")
        refreshdata()
    End Sub
#End Region

#Region "Delete Data"
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            If grdtable.SelectedRows.Count = 0 Then
                Exit Sub
            End If
            If MessageBox.Show("Are you want to delete", "Delete Confirme", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
                Exit Sub
            End If
            Dim row As DataGridViewRow = Me.grdtable.SelectedRows(0)
            Dim id = row.Cells("tblid").Value
            cmd.Connection = cn
            cmd.CommandText = "delete TblTable where tableID=" & id
            cmd.ExecuteNonQuery()
            refreshdata()
        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try
    End Sub
#End Region

#Region "Code for Get value from datagridview in to TexBox in order to Update"

    Private Sub table_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdtable.SelectionChanged
        Try
            If grdtable.SelectedRows.Count = 0 Then
                Exit Sub
            End If
            Dim row As DataGridViewRow = Me.grdtable.SelectedRows(0)
            Me.txtid.Text = row.Cells("tblid").Value
            Me.txttablename.Text = row.Cells("tblname").Value
            Me.txtdescription.Text = row.Cells("tbldiscription").Value

        Catch ex As Exception
        End Try
    End Sub
#End Region

#Region "Clear data Get out from TexBox"

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        txtdescription.Clear()
        txtid.Clear()
        txttablename.Clear()
    End Sub
#End Region

End Class